package main

func reverKGroup(head *ListNode, k int) *ListNode {
	node := head
	for i := 0; i < k; i++ {
		if node == nil {
			return head
		}
		node = node.next
	}
	newHead := reverse(head, node)
	head.next = reverKGroup(node, k)
	return newHead
}

func reverse(first *ListNode, last *ListNode) *ListNode {
	prev := last
	for first != last {
		tmp := first.next
		first.next = prev
		prev = first
		first = tmp
	}
	return prev
}
